﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-13 12:49:22
// 负责人：lvsc
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
using OF.Component.DTO;

namespace OF.Component.DAL
{
	
	/// <summary>
	///二级业务类型 数据访问层
	/// </summary>	
	internal partial class BizType
	{		
    
        /// <summary>
    	/// 根据断言条件获取单个二级业务类型
    	/// </summary>
        public BizTypeInfo QuerySingle(Expression<Func<BizTypeInfo, bool>> predicate)
	    {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }
        
        /// <summary>
    	/// 根据断言条件获获取二级业务类型列表
    	/// </summary>
	    public IList<BizTypeInfo> QueryList(Expression<Func<BizTypeInfo, bool>> predicate)
	    {
	        return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
	    }

        /// <summary>
        /// 根据排序查询业务类型
        /// </summary>
        /// <param name="masterBizTypeId"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public List<BizTypeInfo> GetBizTypes(int masterBizTypeId, int sort)
        {
            List<BizTypeInfo> infos = new List<BizTypeInfo>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM BizType WHERE MasterBizTypeID=@MasterBizTypeID AND IsActive=1");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@MasterBizTypeID", DbType.Int32, masterBizTypeId);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    infos.Add(GetByReader(dr));
                }
            }
            return infos;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public IList<BizTypeDto> SearchList(BasePageConditionFront condition)
        {
            List<BizTypeDto> list = new List<BizTypeDto>();
            string strSql = @"SELECT * FROM (SELECT  ROW_NUMBER() OVER(ORDER BY o.CreatedDate DESC) AS RowNum
                            ,o.*,m.MasterBizTypeName
                            FROM BizType o WITH(NOLOCK) INNER JOIN  MasterBizType m WITH(NOLOCK) ON o.MasterBizTypeID=m.MasterBizTypeID
                            where o.IsActive=1 and m.IsActive=1) AS T WHERE rownum BETWEEN (@PageIndex-1)*@PageSize+1 AND @PageIndex*@PageSize;";
            strSql += @"SELECT count(1) FROM BizType o WITH(NOLOCK) INNER JOIN  MasterBizType m WITH(NOLOCK) ON o.MasterBizTypeID=m.MasterBizTypeID
                            where o.IsActive = 1 and m.IsActive = 1";

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@PageIndex", DbType.Int32, condition.page);
            db.AddInParameter(dbc, "@PageSize", DbType.Int32, condition.rows);
            DataSet ds = db.ExecuteDataSet(dbc);
            list = SQLServerProvider.CreateList<BizTypeDto>(ds.Tables[0]);
            condition.total = int.Parse(ds.Tables[1].Rows[0][0].ToString());
            return list;
        }


        /// <summary>
        /// 根据城市获取首页显示的业务
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
       public DataTable GetIndexBizType(int cityId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT T.BizTypeID,T.BizTypeIcon,T.BizTypePinYin,T.BizTypeName,T.BizTypeSort,T.MasterBizTypeID,T1.MasterBizTypeName FROM BizType T WITH(NOLOCK) ");
            strSql.Append("INNER JOIN MasterBizType T1 WITH(NOLOCK)  ON T1.MasterBizTypeID = T.MasterBizTypeID ");
            strSql.Append("INNER JOIN BizTypeCityRela T2 WITH(NOLOCK) ON T.BizTypeID = T2.BizTypeID ");
            strSql.Append("WHERE T.IsActive = 1 AND T.IsShowIndex = 1 AND T2.CityID = @CityID ORDER BY T.MasterBizTypeID,T.BizTypeSort ASC");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@CityID", DbType.Int32, cityId);
            DataSet ds = db.ExecuteDataSet(dbc);
            return ds.Tables[0];
        }
    }
	
}